<?php

class PecosaController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
    }

    public function indexAction()
    {
     	# $this->_helper->layout->disableLayout ();
		//$this->_helper->viewRenderer->setNoRender ();
		 echo "indexActionpecosa";
	  
    }
	
	public function pecosaAction(){
		echo $this->view->util()->registerScriptJSController($this->getRequest());
	}
	public function buscarAction(){
		$this->_helper->getHelper ( 'ajaxContext' )->initContext ();
		
		if ($this->getRequest ()->isXmlHttpRequest ()) {
			$this->_helper->viewRenderer->setNoRender ();
			$this->_helper->layout->disableLayout ();
			
			$upper = new Zend_Filter_StringToUpper();
			$upper->setEncoding('UTF-8');
			$tip = $this->_request->getPost('tip');
			$val = $upper->filter($this->_request->getPost('val'));
			$chk = $this->_request->getPost('chk');
			$cn = new Model_Data();
			$parameter[]=array("@pcod_pecosa",($tip=='0' ? $val:''));
			$parameter[]=array("@pds_perselab",($tip=='1' ? $val:''));
			if ($tip=='2') $parameter[]=array("@pfech_ingreso",$val);
			$parameter[]=array("@pest_pecosa",$chk);
			/*@pcod_pecosa as varchar(5),  
				@pds_perselab as varchar(150), 
				@pfech_ingreso date=null,  
				@pest_pecosa*/
			$result_bus = $cn ->ejec_store_procedura_sql("mpecosa_list", $parameter);

			if($result_bus==null || count($result_bus)==0){
				echo "Sin Resultados";
			}else{
				$tb="";
				foreach ($result_bus as $value) {
					$tb .= '<tr id="'.$value[0].'"  data="{est_atencion:\''.$value[5].'\',est_aprob:\''.$value[9].'\'}">';
					$tb .='<td>'.$value[0].'</td>';
					$tb .='<td>'.$value[2].'</td>';
					$tb .='<td>'.$value[3].'</td>';
					$tb .='<td>'.$value[6].'</td>';
					$tb .='<td>'.$value[10].'</td>';
					$tb .= '</tr>';
				}
				//echo '<table border="1"><tr><td>';
				echo '<table id="tbl_result" class="ui-widget ui-widget-content tablesorter">'.
						'<thead>'.
							'<tr class="ui-widget-header">'.
								'<tr class="ui-widget-header">'.
									'<th width="100px">Codido</th>'.
									'<th width="230px">Solicitante</th>'.
									'<th width="100px">Fecha Req</th>'.
									'<th width="110px">Est.Atencion</th>'.
									'<th width="130px">Est.Aprobacion</th>'.
								'</tr>'.
							'</tr>'.
						'</thead>'.
						'<tbody>'.
							$tb.
						'</tbody>'.
					'</table>';

				//echo "</td></tr><tr><td>";
				echo $this->view->TableSorter()->ScriptDivPage();
				//echo $this->view->util()->registerScriptJSController($this->getRequest());
				//echo "</td></tr></table>";
				//$(\"#tbl_result\").tablesorter({sortList:[[0,0]], widgets: ['zebra'],headers: { 5:{sorter: false}}}).tablesorterPager({container: $(\"#pager\")});
				echo "<script type=\"text/javascript\">
						$(function() {		
							$(\"#tbl_result\").tablesorter({sortList:[[0,0]]})
							$(\"#tbl_result\").tablesorterPager({
								container: $(\"#pager\"),
								afterPagingCallBack: function(){
									AbrirMenu();
								}
								});
							
						});	
					</script>";
				
				
			}
			
		}
	}
	
	public function mantenimientoAction(){
		
     	echo $this->view->util()->registerScriptJSController($this->getRequest());
		$this->_helper->layout->disableLayout ();
		#$this->_helper->viewRenderer->setNoRender ();
		$type = $this->_request->getParam('type', '');
		$cod = $this->_request->getParam('codItem', '');
		$cn = new Model_Data();
		$pintar = New Libreria_Pintar();
		
		$parameter[]=array("@pcod_pecosa",$cod);
		$parameter[]=array("@pds_perselab",'');
		$parameter[]=array("@pest_pecosa",'');

		$CBparameter[]=array("@pcod_combin","");
		$CBparameter[]=array("@pdescripcion",'');
		$CBparameter[]=array("@pest_combin",'');//Poner en 1!!!

		$CBresult_bus = $cn ->ejec_store_procedura_sql("Mcombinaciones_list", $CBparameter);
		for ($x=0;$x<count($CBresult_bus);$x++)
    		$cboCB[] = array($CBresult_bus[$x][0],$CBresult_bus[$x][1]);

		if ($type=='N'){
			if (count($CBresult_bus)!=0){
				//$val[] = array('#cod_combin',$pintar->ContenidoCombo($cboCB,'null'),'html');
				$val[] = array('#cod_combin','<option value="9999999999">Selecionar MP</option>','html');
				$pintar->PintarValor($val);
			}
		}elseif($type=='M' || $type=='E'){
			$result_bus = $cn ->ejec_store_procedura_sql("mpecosa_list", $parameter);
			$val[] = array('#hdcodigo',$result_bus[0][0],'val');
			$val[] = array('#hd_cod_pers',$result_bus[0][1],'val');
			$val[] = array('#txtcod_perselab',$result_bus[0][2],'val');
			$val[] = array('#txtfech_ing',$result_bus[0][3],'val');
			$val[] = array('#txtds_obs',$result_bus[0][11],'val');
			//Dpecosa_list
			
			$parameterdt[]=array("@pcod_dpecosa",'');
			$parameterdt[]=array("@pcod_pecosa",$cod);
			$parameterdt[]=array("@pest_dpecosa",'1');
			/*@pcod_dpecosa as varchar(5),      
				@pcod_pecosa as varchar(5),
				@pest_dpecosa*/
			$result_det = $cn ->ejec_store_procedura_sql("Dpecosa_list", $parameterdt);
			$detallemp='';
			if (count($result_det)==0){
				$val[] = array('#cod_combin','<option value="9999999999">Selecionar MP</option>','html');
			}else{
			for ($i=0;$i<count($result_det);$i++){
				$detallemp .=
				'<tr >'.
					'<td>'.
							'<input type="hidden" id="cod_mateprima" name="det['.$i.'][cod_mateprima]" value="'.$result_det[$i][2].'">'.
							'<input  name="det['.$i.'][text_mateprima]" id="text_mateprima" type="text" value="'.$result_det[$i][3].'" style="width: 200px" class="ui-widget-content ui-corner-all busmantprima">'.
					'</td>'.
					'<td>'.
						'<select  name="det['.$i.'][cod_combin]" id="cod_combin" style="width: 210px" class="ui-widget-content ui-corner-all cod_combin">'.$pintar->ContenidoCombo($cboCB,$result_det[$i][4]).'</select>'.							
					'</td> '.
					'<td>'.
					'<input  name="det['.$i.'][cant]" id="cant" type="text" class="num"  style="width: 70px" value="'.$result_det[$i][5].'"  class="ui-widget-content ui-corner-all">'.
					'<td>'.
					'<td style="display:none" ><input  name="det['.$i.'][tblcodigo]" id="tblcodigo" type="text" value="'.$result_det[$i][0].'"></td>'.
				'</tr>';
			}
				$val[] = array('#tbl_detallePeco tbody',$detallemp,'html');
				
			}


			$pintar->PintarValor($val);
		}
		
		echo '<script type="text/javascript">'.
						'$(function() {	'.
							'$("#tbl_detallePeco").grider({'.
								'countRow : true,'.
								'countRowAdd : true,'.
								'afterPagingCallBack: function(){'.
									'callbackbusmatprima();'.
								'}'.
							"});".
						'});'.
		"</script>";
	}

	public function executemanteAction(){
		$this->_helper->getHelper ( 'ajaxContext' )->initContext ();
		
		if ($this->getRequest ()->isXmlHttpRequest ()) {
			$this->_helper->viewRenderer->setNoRender ();
			$this->_helper->layout->disableLayout ();
			
			$upper = new Zend_Filter_StringToUpper();
			$upper->setEncoding('UTF-8');
			$tip = $this->_request->getPost('type');
			$hdcodigo = $upper->filter($this->_request->getPost('hdcodigo'));
			$hd_cod_pers = $upper->filter($this->_request->getPost('hd_cod_pers'));
			$txtfech_ing = $upper->filter($this->_request->getPost('txtfech_ing'));
			$txtds_obs = $upper->filter($this->_request->getPost('txtds_obs'));
			$st_estado = $upper->filter($this->_request->getPost('st_stado'));
			$arrtable = $this->_request->getPost('det');
			$cn = new Model_Data();
				/*MPecosa_InsertUpdate    
				@ptype char(1),      
				@pcod_pecosa  char(5),      
				@pcod_perselab  varchar(150),      
				@pfech_ingreso varchar(250),      
				@pfech_atencion varchar(250),      
				@pest_atencion varchar(250),
				@pcod_persaprob  char(1),     
				@pest_aprob varchar(max),
				@pest_pecosa varchar(max),
				@pobs_pecosa varchar(max),
				@pdetalle  varchar(max),
				@puser_reg  varchar(25),      
				@phost_reg*/
			if($tip=='M' ||$tip=='N' ){
				$parameter[]=array("@ptype",$tip);
				$parameter[]=array("@pcod_pecosa",$hdcodigo);
				$parameter[]=array("@pcod_perselab",$hd_cod_pers);
				$parameter[]=array("@pfech_ingreso",$txtfech_ing);
				$parameter[]=array("@pobs_pecosa" ,$txtds_obs);
				//$parameter[]=array("@pfech_atencion" ,null);
				//$parameter[]=array("@pest_atencion" ,0);//0 Penciente 1 atendido 2 parcial
				//$parameter[]=array("@pcod_persaprob" ,null);
				//$parameter[]=array("@pest_aprob" ,'0');//0 Penciente 1 aprob 2 desapro
				$parameter[]=array("@pest_pecosa" ,$st_estado);
				$parameter[]=array("@puser_reg",$this->view->util()->getUser());
				$parameter[]=array("@phost_reg",$this->view->util()->getHost());
				$xml='';
				$i=1;
				/* xcodigo varchar(max)  ,    
					  xcorrela varchar(max)  ,    
					  xcod_pecosa varchar(max)  ,    
					  xcod_matpri varchar(max)  ,    
					  xcod_combinac varchar(max),
					  xctd_solic varchar(max)*/
					 
				foreach ($arrtable as $value) {
					$xml.='';
					$xml .='<r ';
					$xml .='xcodigo="'.$value["tblcodigo"].'" ';
					$xml .='xcorrela="'.($i).'" ';
					$xml .='xcod_pecosa="'.$hdcodigo.'" ';
					$xml .='xcod_matpri="'.$value["cod_mateprima"].'" ';
					$xml .='xcod_combinac="'.$value["cod_combin"].'" ';
					$xml .='xctd_solic="'.$value["cant"].'" ';
					$xml .='>';
					$xml .='</r>';
					$i++;
				}
				
				$xml="<d>".$xml."</d>";
				//echo "<textarea>".$xml."</textarea>";
				$parameter[]=array("@pdetalle",$xml);
				$result = $cn ->ejec_store_procedura_sql("MPecosa_InsertUpdate", $parameter);
				 	
			}elseif($tip=='D'){
	
				$parameter[]=array("@pcod_present",$cod_present);
				$parameter[]=array("@puser_reg",$this->view->util()->getUser());
				$parameter[]=array("@phost_reg",$this->view->util()->getHost());
				$result = $cn ->ejec_store_procedura_sql("MPecosa_Delete", $parameter);
			}
			if($result[0][0]=='1') echo "Transaccion Exitosa";
			if($result[0][0]=='2') 
			 echo  $this->view->util()->getTextError($result[0]);
		}
		
	}

	public function administracionAction(){
		echo $this->view->util()->registerScriptJSController($this->getRequest());
	}
	
	public function adminbuscarAction(){
		$this->_helper->getHelper ( 'ajaxContext' )->initContext ();
		
		if ($this->getRequest ()->isXmlHttpRequest ()) {
			$this->_helper->viewRenderer->setNoRender ();
			$this->_helper->layout->disableLayout ();
			
			$upper = new Zend_Filter_StringToUpper();
			$upper->setEncoding('UTF-8');
			$tip = $this->_request->getPost('tip');
			$val = $upper->filter($this->_request->getPost('val'));
			
			$cn = new Model_Data();
			$parameter[]=array("@pcod_pecosa",($tip=='0' ? $val:''));
			$parameter[]=array("@pds_perselab",($tip=='1' ? $val:''));
			if ($tip=='2') $parameter[]=array("@pfech_ingreso",$val);
			$parameter[]=array("@pest_pecosa",'1');
			/*@pcod_pecosa as varchar(5),  
				@pds_perselab as varchar(150), 
				@pfech_ingreso date=null,  
				@pest_pecosa*/
			$result_bus = $cn ->ejec_store_procedura_sql("mpecosa_list", $parameter);

			if($result_bus==null || count($result_bus)==0){
				echo "Sin Resultados";
			}else{
				$tb="";
				foreach ($result_bus as $value) {
					$tb .= '<tr id="'.$value[0].'"  data="{est_atencion:\''.$value[5].'\'}">';
					$tb .='<td><input type="checkbox" id="'.$value[0].' class="checkbox" ></td>';
					$tb .='<td>'.$value[0].'</td>';
					$tb .='<td>'.$value[2].'</td>';
					$tb .='<td>'.$value[3].'</td>';
					$tb .='<td>'.$value[6].'</td>';
					$tb .='<td>'.$value[10].'</td>';
					$tb .= '</tr>';
				}
				//echo '<table border="1"><tr><td>';
				echo '<table id="tbl_result" class="ui-widget ui-widget-content tablesorter">'.
						'<thead>'.
							'<tr class="ui-widget-header">'.
								'<tr class="ui-widget-header">'.
									'<th width="20px">&nbsp;&nbsp;</th>'.
									'<th width="100px">Codido</th>'.
									'<th width="150px">Solicitante</th>'.
									'<th width="100px">Fecha Req</th>'.
									'<th width="130px">Est.Atencion</th>'.
									'<th width="100px">Est.Aprobacion</th>'.
								'</tr>'.
							'</tr>'.
						'</thead>'.
						'<tbody>'.
							$tb.
						'</tbody>'.
					'</table>';

				//echo "</td></tr><tr><td>";
				echo $this->view->TableSorter()->ScriptDivPage();
				//echo $this->view->util()->registerScriptJSController($this->getRequest());
				//echo "</td></tr></table>";
				//$(\"#tbl_result\").tablesorter({sortList:[[0,0]], widgets: ['zebra'],headers: { 5:{sorter: false}}}).tablesorterPager({container: $(\"#pager\")});
				echo "<script type=\"text/javascript\">
						$(function() {		
							$(\"#tbl_result\").tablesorter({sortList:[[0,0]]})
							$(\"#tbl_result\").tablesorterPager({
								container: $(\"#pager\"),
								afterPagingCallBack: function(){
									AbrirMenu();
								}
								});
							
						});	
					</script>";
				
				
			}
			
		}
	}
	
	public function adminexecuteAction(){
		$this->_helper->getHelper ( 'ajaxContext' )->initContext ();
		
		if ($this->getRequest ()->isXmlHttpRequest ()) {
			$this->_helper->viewRenderer->setNoRender ();
			$this->_helper->layout->disableLayout ();
			
			$upper = new Zend_Filter_StringToUpper();
			$upper->setEncoding('UTF-8');
			$tip = $this->_request->getPost('type');
			$pcod_pecosa = $upper->filter($this->_request->getPost('pcod_pecosa'));
			$admintype = $upper->filter($this->_request->getPost('admintype'));
			$cn = new Model_Data();
				/*@ptype smallint,
					@pcod_pecosa char(5),
					@pcod_persaprob char(5)=null,
					@pest_aprob char(1)=null,
					@puser_reg  varchar(25),      
					@phost_reg  varchar(25)*/
		
				$parameter[]=array("@ptype",1);
				$parameter[]=array("@pcod_pecosa",$pcod_pecosa);
				$parameter[]=array("@pcod_persaprob",$this->view->util()->getcodPerson());
				$parameter[]=array("@pest_aprob",($admintype=='A' ? '1' : '2'));
				$parameter[]=array("@puser_reg",$this->view->util()->getUser());
				$parameter[]=array("@phost_reg",$this->view->util()->getHost());
	
				$result = $cn ->ejec_store_procedura_sql("MPecosaAdministrar", $parameter);
				 	
		
			if($result[0][0]=='1') echo "Transaccion Exitosa";
			if($result[0][0]=='2') 
			 echo  $this->view->util()->getTextError($result[0]);
		}
		
	}
	public function atenderAction(){
		echo $this->view->util()->registerScriptJSController($this->getRequest());	
	}
	public function atenderbuscarAction(){
		$this->_helper->getHelper ( 'ajaxContext' )->initContext ();
		
		if ($this->getRequest ()->isXmlHttpRequest ()) {
			$this->_helper->viewRenderer->setNoRender ();
			$this->_helper->layout->disableLayout ();
			
			$upper = new Zend_Filter_StringToUpper();
			$upper->setEncoding('UTF-8');
			$tip = $this->_request->getPost('tip');
			$val = $upper->filter($this->_request->getPost('val'));
			
			$cn = new Model_Data();
			$parameter[]=array("@pcod_pecosa",($tip=='0' ? $val:''));
			$parameter[]=array("@pds_perselab",($tip=='1' ? $val:''));
			if ($tip=='2') $parameter[]=array("@pfech_ingreso",$val);
			$parameter[]=array("@pest_pecosa",'1');
			$parameter[]=array("@pest_aprob",'1');//1 = Aprobado
			/*@pcod_pecosa as varchar(5),  
				@pds_perselab as varchar(150), 
				@pfech_ingreso date=null,  
				@pest_pecosa*/
			$result_bus = $cn ->ejec_store_procedura_sql("mpecosa_list", $parameter);

			if($result_bus==null || count($result_bus)==0){
				echo "Sin Resultados";
			}else{
				$tb="";
				foreach ($result_bus as $value) {
					$tb .= '<tr id="'.$value[0].'" data="{est_atencion:\''.$value[5].'\'}">';
					$tb .='<td>'.$value[0].'</td>';
					$tb .='<td>'.$value[2].'</td>';
					$tb .='<td>'.$value[3].'</td>';
					$tb .='<td>'.$value[6].'</td>';
					$tb .='<td>'.$value[10].'</td>';
					$tb .= '</tr>';
				}
				//echo '<table border="1"><tr><td>';
				echo '<table id="tbl_result" class="ui-widget ui-widget-content tablesorter">'.
						'<thead>'.
							'<tr class="ui-widget-header">'.
								'<tr class="ui-widget-header">'.
									'<th width="100px">Codido</th>'.
									'<th width="150px">Solicitante</th>'.
									'<th width="100px">Fecha Req</th>'.
									'<th width="130px">Est.Atencion</th>'.
									'<th width="100px">Est.Aprobacion</th>'.
								'</tr>'.
							'</tr>'.
						'</thead>'.
						'<tbody>'.
							$tb.
						'</tbody>'.
					'</table>';

				//echo "</td></tr><tr><td>";
				echo $this->view->TableSorter()->ScriptDivPage();
				//echo $this->view->util()->registerScriptJSController($this->getRequest());
				//echo "</td></tr></table>";
				//$(\"#tbl_result\").tablesorter({sortList:[[0,0]], widgets: ['zebra'],headers: { 5:{sorter: false}}}).tablesorterPager({container: $(\"#pager\")});
				echo "<script type=\"text/javascript\">
						$(function() {		
							$(\"#tbl_result\").tablesorter({sortList:[[0,0]]})
							$(\"#tbl_result\").tablesorterPager({
								container: $(\"#pager\"),
								afterPagingCallBack: function(){
									AbrirMenu();
								}
								});
							
						});	
					</script>";
				
				
			}
			
		}
	}
	
	public function atenderventanaAction(){
		echo $this->view->util()->registerScriptJSController($this->getRequest());
		$this->_helper->layout->disableLayout ();
		#$this->_helper->viewRenderer->setNoRender ();
		$type = $this->_request->getParam('type', '');
		$cod = $this->_request->getParam('codItem', '');
		$cn = new Model_Data();
		$pintar = New Libreria_Pintar();
		
		$parameter[]=array("@pcod_pecosa",$cod);
		$parameter[]=array("@pds_perselab",'');
		$parameter[]=array("@pest_pecosa",'');

		$CBparameter[]=array("@pcod_combin","");
		$CBparameter[]=array("@pdescripcion",'');
		$CBparameter[]=array("@pest_combin",'');//Poner en 1!!!

		$CBresult_bus = $cn ->ejec_store_procedura_sql("Mcombinaciones_list", $CBparameter);
		for ($x=0;$x<count($CBresult_bus);$x++)
    		$cboCB[] = array($CBresult_bus[$x][0],$CBresult_bus[$x][1]);

			$result_bus = $cn ->ejec_store_procedura_sql("mpecosa_list", $parameter);
			$val[] = array('#hdcodigo',$result_bus[0][0],'val');
			$val[] = array('#hd_cod_pers',$result_bus[0][1],'val');
			$val[] = array('#txtcod_perselab',$result_bus[0][2],'val');
			$val[] = array('#txtfech_ing',$result_bus[0][3],'val');
			$val[] = array('#txtds_obs',$result_bus[0][11],'val');
			//Dpecosa_list
			$hab[] = array("#txtcod_perselab",true);
			$hab[] = array("#txtfech_ing",true);
			$parameterdt[]=array("@pcod_dpecosa",'');
			$parameterdt[]=array("@pcod_pecosa",$cod);
			$parameterdt[]=array("@pest_dpecosa",'1');
			
			$result_det = $cn ->ejec_store_procedura_sql("Dpecosa_list", $parameterdt);
			$detallemp='';
			if (count($result_det)==0){
				$val[] = array('#cod_combin','<option value="9999999999">....</option>','html');
			}else{
			for ($i=0;$i<count($result_det);$i++){
				$detallemp .=
				'<tr >'.
					'<td>'.
							'<input type="hidden" id="cod_mateprima" name="det['.$i.'][cod_mateprima]" value="'.$result_det[$i][2].'">'.
							'<input  name="det['.$i.'][text_mateprima]" id="text_mateprima" type="text" value="'.$result_det[$i][3].'" readonly="true" style="width: 200px" class="ui-widget-content ui-corner-all busmantprima ui-state-disabled">'.
					'</td>'.
					'<td>'.
						'<select  name="det['.$i.'][cod_combin]" id="cod_combin" style="width: 210px" class="ui-widget-content ui-corner-all cod_combin ui-state-disabled" readonly="true">'.$pintar->ContenidoCombo($cboCB,$result_det[$i][4]).'</select>'.							
					'</td> '.
					'<td>'.
					'<input  name="det['.$i.'][cant]" id="cant" type="text"  style="width: 70px" value="'.$result_det[$i][5].'"  class="ui-widget-content ui-corner-all num ui-state-disabled" readonly="true">'.
					'</td>'.
					'<td>'.
						'<input type="hidden" id="cod_div" name="det[0][cod_div]">'.
						'<input  name="txt_lugaralmacen" id="txt_lugaralmacen" type="text" value=""  class="ui-widget-content ui-corner-all ui-state-disabled" readonly="true">'.							
					'</td> '.
					'<td>'.
							'<button class="ui-state-default ui-corner-all actionbuscar" style="cursor: pointer;" >'.
							'<span class="ui-icon ui-icon-search"></span>'.
							'</button>'.
					'</td>'.
					'<td>'.
					'<input  name="det['.$i.'][cantatend]" id="cant" type="text" style="width: 70px" value="0"  class="ui-widget-content ui-corner-all num">'.
					'</td>'.
					'<td style="display:none" ><input  name="det['.$i.'][tblcodigo]" id="tblcodigo" type="text" value="'.$result_det[$i][0].'"></td>'.
				'</tr>';
			}
				$val[] = array('#tbl_detalleAtend tbody',$detallemp,'html');
				
			}

			$function[] =array("actionBuscar()");
			$pintar->PintarValor($val);
			$pintar->EjecutarFuncion($function);
			$pintar->ComponenteSoloLectura($hab);
		
		
	} 
	public function atenderexecuteAction(){
		/*if($result[0][0]=='1') echo "Transaccion Exitosa";
			if($result[0][0]=='2') 
			 echo  $this->view->util()->getTextError($result[0]);*/
	}
}

